package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.UserServiceAdmin;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.HealthSummaryYearMapper;
import com.dwxt.cdms.entity.*;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.util.PartyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HealthSummaryYearService {
    @Autowired
    private HealthSummaryYearMapper healthSummaryYearMapper;

    @Autowired
    private HealthSummaryService healthSummaryService;

    @Autowired
    private StationService stationService;

    @Autowired
    private UserServiceAdmin userServiceAdmin;

    public BaseResult selectHealthSummaryYearPage(HealthSummaryYear healthSummaryYear, Page page, Integer type) {
        HealthSummaryYearExample example = new HealthSummaryYearExample();
        List<Role> roles = userServiceAdmin.selectRolesByUserId(PartyUtil.getCurrentUserId());
        //superadmin
        if (roles.stream().anyMatch(role -> role.getId().equals(1))) {
            example.setPermission(1);
            //客户专员（区域管理）
        } else if (roles.stream().anyMatch(role -> role.getId().equals(9))) {
            example.setPermission(9);
            Integer stationId = stationService.getLoginUserStation();
            if (stationId != null) {
                example.setStationId(stationId);
            }
            //医生、营养师、健康管理员
        } else {
            example.setPermission(0);
            AdminUser user = userServiceAdmin.selectUserById(Integer.valueOf(PartyUtil.getCurrentUserId()));
            example.setResponsibleId(user.getId());
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        example.setOrderByClause("create_time desc");
        HealthSummaryYearExample.Criteria criteria = example.createCriteria();

        if (healthSummaryYear.getSerialNumber() != null) {
            criteria.andSerialNumberLike("%" + healthSummaryYear.getSerialNumber() + "%");
        }
        if (StringUtils.isNotBlank(healthSummaryYear.getCustomerName())) {
            criteria.andCustomerNameLike("%" + healthSummaryYear.getCustomerName() + "%");
        }
        if (StringUtils.isNotBlank(healthSummaryYear.getPhone())) {
            criteria.andPhoneLike("%" + healthSummaryYear.getPhone() + "%");
        }
        if (healthSummaryYear.getCustomerId() != null) {
            criteria.andCustomerIdEqualTo(healthSummaryYear.getCustomerId());
        }
        if (type == null || type == 1) {
            criteria.andIsSureEqualTo(YesOrNo.NO);
        } else if (type == 2) {
            //小程序
            criteria.andIsSureEqualTo(YesOrNo.YES);
        }

        List<HealthSummaryYear> healthSummaries = healthSummaryYearMapper.selectByExampleWithBLOBs(example);
        PageInfo pageInfo = new PageInfo(healthSummaries);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult updateHealthSummaryYear(HealthSummaryYear healthSummaryYear) {
        healthSummaryYear.setIsSure(YesOrNo.YES);
        healthSummaryYearMapper.updateByPrimaryKeySelective(healthSummaryYear);
        return BaseResult.ok();
    }

    public HealthSummaryYear selectLastRecord(Integer customerId) {
        return healthSummaryYearMapper.selectLastRecord(customerId);
    }

    public void insertSelective(HealthSummaryYear healthSummaryYear) {
        healthSummaryYearMapper.insertSelective(healthSummaryYear);
    }

    public List<HealthSummary> selectHealthSummaryById(Integer id) {
        HealthSummaryYear healthSummaryYear = healthSummaryYearMapper.selectByPrimaryKey(id);
        Integer start = (healthSummaryYear.getCount() - 1) * 4 + 1;
        Integer end = healthSummaryYear.getCount() * 4;
        List<HealthSummary> list = healthSummaryService.selectRecentRecord(start, end, healthSummaryYear.getCustomerId());
        return list;
    }

    public BaseResult selectPreviousRecord(Integer id) {
        HealthSummaryYear healthSummaryYear = healthSummaryYearMapper.selectByPrimaryKey(id);
        HealthSummaryYearExample example = new HealthSummaryYearExample();
        example.createCriteria().andCountEqualTo(healthSummaryYear.getCount() - 1).andCustomerIdEqualTo(healthSummaryYear.getCustomerId());
        List<HealthSummaryYear> healthSummaryYears = healthSummaryYearMapper.selectByExampleWithBLOBs(example);
        return BaseResult.ok(healthSummaryYears.size() > 0 ? healthSummaryYears.get(0) : null);
    }

    public BaseResult selectHistoryPage(Page page, Integer id) {
        HealthSummaryYear healthSummary = healthSummaryYearMapper.selectByPrimaryKey(id);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        HealthSummaryYearExample example = new HealthSummaryYearExample();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andCustomerIdEqualTo(healthSummary.getCustomerId()).andIdNotEqualTo(healthSummary.getId());
        List<HealthSummaryYear> list = healthSummaryYearMapper.selectByExampleWithBLOBs(example);
        return BaseResult.ok(new PageInfo<>(list));
    }

    public BaseResult selectHealthSummaryByCustomerId(Page page,Integer customerId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        HealthSummaryYearExample example = new HealthSummaryYearExample();
        HealthSummaryYearExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        List<HealthSummaryYear> list = healthSummaryYearMapper.selectByExample(example);
        PageInfo<HealthSummaryYear> pageInfo = new PageInfo<>(list);
        return BaseResult.ok(pageInfo);
    }
}
